package cn.edu.ecnu.gis.client.gadgets;

import cn.edu.ecnu.gis.client.model.Gadget;
import cn.edu.ecnu.gis.client.model.GadgetClass;
import cn.edu.ecnu.gis.client.view.EditLabel;
import cn.edu.ecnu.gis.client.view.GadgetContainerView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.Text;


public class ToDoListGadget extends Gadget{
	
	public static class Class extends GadgetClass {
		public Class(){
			super("任务列表", false);
		}
		public Gadget newGadget(){
			return new ToDoListGadget(this);
		}
	}

	private VerticalPanel list = new VerticalPanel();
	protected ToDoListGadget(Class c){
		super(c);
		initWidget( list );
		list.setWidth("100%");
		Hyperlink newToDo = new Hyperlink("新建任务","");
		newToDo.addClickListener( new ClickListener(){
			public void onClick( Widget sender )
			{
				addToDo( "新建任务 " + list.getWidgetCount(),true, false );
				getGadgetListener().onInterfaceChange();
			}
		});
		list.add( newToDo );
	}
	
	public void addToDo( String text, boolean edit, boolean complete ){
		HorizontalPanel row = new HorizontalPanel();
		final CheckBox checkBox = new CheckBox();
		final EditLabel label = new EditLabel( text );
		final PushButton delete = new PushButton(GadgetContainerView.titleBarImages.closeIcon().createImage());
		checkBox.addClickListener( new ClickListener(){
			public void onClick( Widget sender ){
				if( checkBox.isChecked() )
					label.addStyleName("ToDoList-Done");
				else
					label.removeStyleName("ToDoList-Done");		
				getGadgetListener().onInterfaceChange();
			}
		});
		delete.addClickListener( new ClickListener(){
			public void onClick( Widget sender )
			{
				list.remove( delete.getParent() );
				getGadgetListener().onInterfaceChange();
			}
		});
		
		row.add( checkBox );
		row.add( label );
		row.add( delete );
		row.setCellWidth( label, "100%");
		list.insert( row, list.getWidgetCount()-1 );
		
		if( complete)
		{
			checkBox.setChecked(true);
			label.addStyleName("ToDoList-Done");
		}
		if( edit )
		{
			label.edit();
		}
	}
	
	public void toXML( Element element ){
		super.toXML( element );			
		for( int i=0; i < list.getWidgetCount()-1; i++ ){
			HorizontalPanel row = (HorizontalPanel)list.getWidget(i);
			CheckBox checkBox = (CheckBox)row.getWidget(0);
			EditLabel label = (EditLabel)row.getWidget(1);
			Element toDoItemElement = element.getOwnerDocument().createElement("ToDoItem");
			toDoItemElement.setAttribute("complete",checkBox.isChecked()?"yes":"no");
			Text prefText = element.getOwnerDocument().createTextNode(label.getText());
			toDoItemElement.appendChild(prefText);
			element.appendChild(toDoItemElement);
		}
	}
	
	public void fromXML( Element element ){
		super.fromXML(element);
		NodeList toDoItems = element.getElementsByTagName("ToDoItem");
		for( int i = 0; i < toDoItems.getLength(); i++ ){
			Element toDoItemElement = (Element)toDoItems.item(i);
			boolean complete = toDoItemElement.getAttribute("complete").compareTo("yes")==0;
			Text toDoItemValue = (Text)toDoItemElement.getChildNodes().item(0);
			addToDo( toDoItemValue.getNodeValue(), false, complete );
		}
	}
	public interface BoundImage extends ImageBundle {

        AbstractImagePrototype inbox();
    }

   
    public AbstractImagePrototype getBoundImage() {
        BoundImage images = (BoundImage) GWT.create(BoundImage.class);
        return images.inbox();
    }
}
